package io.vector.study.springboot.api;

import io.vector.study.springboot.entity.User;
import io.vector.study.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * Created by vector on 2017/4/28.
 */
@RestController
@RequestMapping("/users")
public class UserApi extends BaseApi {

    @Autowired
    private IUserService userService;

    @GetMapping(value = "", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object getUsers() {
        return ApiResponse.page(userService.selectPage(extractPageFromRequest()));
    }

    @PostMapping(value = "", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object createUser(@RequestBody User user) {
        userService.insert(user);
        return ApiResponse.success();
    }

    @GetMapping("/{username}")
    public String sayHello(@PathVariable String username) {
        return String.format("Hello %s!", username);
    }


}
